/*
RD GRAPHS

Data 	: 
Folder 	: 
Date	: 2018-03-10

Creator		: Jonas Cederlof	(JC)
Description : 
Notes:


LATEST UPDATE: 	

*/

********************************************************************************

clear
set more		 off
cap   log close 	_all

log using 	"../log/B3_RDgraphs.log"	, replace 
use 		"$datapath/A3_age55data_WC.dta"

*local define
local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
{ // Define globals
*===============================================================================
global covariets 	"C_annual_ear_prenot_def C_female C_immigrant C_tenureatnot C_educ1 C_educ2 C_educ3 "		
global instrument	"treat"
global linear		"runvar inter"
global quadratic	"$linear q_runvar q_inter"
global cubic 		"$quadratic c_runvar c_inter"
}
*

*Generate some variables wihtin 2 years
replace alosdag=0 if alosdag==.
bys lopnr (date) : egen cumatfirm2 	= total(atfirm2==1) 			if inrange(eventtime_def,1,24) 
bys lopnr (date) : egen cumotherfirm2 	= total(atfirm2==0 & working2==1) 	if inrange(eventtime_def,1,24) 
bys lopnr (date) : egen cumnonemployed 	= total(atfirm2==0 & working2==0) 	if inrange(eventtime_def,1,24) 
bys lopnr (date) : egen cumunemployed 	= total(alosdag>0  & working2==0) 	if inrange(eventtime_def,1,24) 
bys lopnr (date) : egen cumoutofLF 	= total(alosdag==0 & working2==0) 	if inrange(eventtime_def,1,24) 




*Keep relevant time to speed up .do-file
keep if inlist(eventtime_def,0,1,6,7,11,12)

{ // Annual earnings (t+1)
*===============================================================================
preserve
	keep if eventtime_def==12
	local outcome "annual_ear"
	replace `outcome' = `outcome'/1000
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = round(_b[$instrument] ,.001)
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	
	
	gen temp =round(runvar*12)
	egen 	bin = cut(temp), at(-36(2)0)
	egen 	bin2 = cut(temp), at(1(2)37)
	replace bin = bin2 if bin==.
	replace bin = bin + .5 
	bys bin: egen `outcome'_bin = mean(`outcome')
	*tab runvar bin
	
	
	
	collapse (mean) `outcome' `outcome'_bin  (sum) freq, by(runvar bin treat)
	
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 	yscale(r(340 400)) ylab(340(20)400)					///
		  xtitle("Normalized age (months)") ytitle("Annual earnings (t+1) (1,000 SEK)") 	///
		  text(350 8   "{&beta}=`b'***" "(`se')"  ,place(right)  )
	graph export "$agegraphpath/RD/RD_`outcome'_t1.pdf", replace
	graph export "$agegraphpath/RD/RD_`outcome'_eps.pdf", replace
restore		
}
*
{ // Employment notifying firm within 2 years 
*===============================================================================
preserve
	keep if eventtime_def==1
	local outcome "cumatfirm2"
	
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = round(_b[$instrument] ,.001)
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	
	
	gen temp =round(runvar*12)
	egen 	bin = cut(temp), at(-36(2)0)
	egen 	bin2 = cut(temp), at(1(2)37)
	replace bin = bin2 if bin==.
	replace bin = bin + .5 
	bys bin: egen `outcome'_bin = mean(`outcome')
	*tab runvar bin
	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	collapse (mean) `outcome' `outcome'_bin  (sum) freq, by(runvar bin treat)
	
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Months worked at notifying firm" "(within 2 years of notification") ///
		   text(9 -14   "{&beta}=`b'***" "(0`se')"  ,place(right)  )
	graph export "$agegraphpath/RD/RD_`outcome'.pdf", replace
restore	
	
}
*
{ // Employment other firm within 2 years 
*===============================================================================
preserve
	keep if eventtime_def==1
	local outcome "cumotherfirm2"
	
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = abs(round(_b[$instrument] ,.001))
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	
	
	gen temp =round(runvar*12)
	egen 	bin = cut(temp), at(-36(2)0)
	egen 	bin2 = cut(temp), at(1(2)37)
	replace bin = bin2 if bin==.
	replace bin = bin + .5 
	bys bin: egen `outcome'_bin = mean(`outcome')
	*tab runvar bin
	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	collapse (mean) `outcome' `outcome'_bin  (sum) freq, by(runvar bin treat)
	
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Months worked at new firm" "(within 2 years of notification") ///
		   text(7.5 -14   "{&beta}=-0`b'" "(0`se')"  ,place(right)  )
	graph export "$agegraphpath/RD/RD_`outcome'.pdf", replace
restore	
	
}
*
{ // Months in non-employment within 2 years 
*===============================================================================
preserve
	keep if eventtime_def==1
	local outcome "cumnonemployed"
	
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = round(_b[$instrument] ,.001)
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	
	
	gen temp =round(runvar*12)
	egen 	bin = cut(temp), at(-36(2)0)
	egen 	bin2 = cut(temp), at(1(2)37)
	replace bin = bin2 if bin==.
	replace bin = bin + .5 
	bys bin: egen `outcome'_bin = mean(`outcome')
	*tab runvar bin
	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	collapse (mean) `outcome' `outcome'_bin  (sum) freq, by(runvar bin treat)
	
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Months in non-employment" "(within 2 years of notification") ///
		   text(7.2 8   "{&beta}=`b'***" "(0`se')"  ,place(right)  )
	graph export "$agegraphpath/RD/RD_`outcome'.pdf", replace
restore	
	
}
*
{ // Months in unemployment within 2 years 
*===============================================================================
preserve
	keep if eventtime_def==1
	local outcome "cumunemployed"
	
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = abs(round(_b[$instrument] ,.001))
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	
	
	gen temp =round(runvar*12)
	egen 	bin = cut(temp), at(-36(2)0)
	egen 	bin2 = cut(temp), at(1(2)37)
	replace bin = bin2 if bin==.
	replace bin = bin + .5 
	bys bin: egen `outcome'_bin = mean(`outcome')
	*tab runvar bin
	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	collapse (mean) `outcome' `outcome'_bin  (sum) freq, by(runvar bin treat)
	
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Months in unemployemnt" "(within 2 years of notification") ///
		   text(5 12   "{&beta}=-0`b'*" "(0`se')"  ,place(right)  )
	graph export "$agegraphpath/RD/RD_`outcome'.pdf", replace
restore	
	
}
*
{ // Months out of LF within 2 years 
*===============================================================================
preserve
	keep if eventtime_def==1
	local outcome "cumoutofLF"
	
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = abs(round(_b[$instrument] ,.001))
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	
	
	gen temp =round(runvar*12)
	egen 	bin = cut(temp), at(-36(2)0)
	egen 	bin2 = cut(temp), at(1(2)37)
	replace bin = bin2 if bin==.
	replace bin = bin + .5 
	bys bin: egen `outcome'_bin = mean(`outcome')
	*tab runvar bin
	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	collapse (mean) `outcome' `outcome'_bin  (sum) freq, by(runvar bin treat)
	
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Months out of labor force" "(within 2 years of notification") ///
		   text(2.5 12   "{&beta}=-0`b'***" "(0`se')"  ,place(right)  )
	graph export "$agegraphpath/RD/RD_`outcome'.pdf", replace
restore	
	
}
*
{ // Pr(At notifying firm | m+6 , m+12)
*===============================================================================

forvalues t = 6(6)12 {
	preserve

		local outcome "atfirm2"

		keep  if eventtime_def==`t'
		*==========
		xtset   ym_def
		*==========

		eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
		local b  = round(_b[$instrument] ,.001)
		local se = round(_se[$instrument] ,.0001)
		
		gen freq=1
			
		*Residulize year*moth fixed effects
		qui areg `outcome' $covariets, absorb(ym_def)
		predict e, residuals
		sum `outcome' 
		replace `outcome' = e + r(mean)
		
		gen temp =round(runvar*12)
		egen 	bin = cut(temp), at(-36(2)0)
		egen 	bin2 = cut(temp), at(1(2)37)
		replace bin = bin2 if bin==.
		replace bin = bin + .5 
		bys bin: egen `outcome'_bin = mean(`outcome')
		*tab runvar bin
		
		collapse (mean) `outcome' `outcome'_bin  (sum) freq, by(runvar bin treat)
		
		replace runvar = runvar*12
		
		*Linear Fit
		twoway 	(scatter `outcome'_bin  bin 		 	 , ms(o) mcolor(black))  		///	 	
			(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
			(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
			, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
			  xscale(r(-36 36)) xlabel(-36(12)36) 	name(t`t',replace)					///
			  xtitle("Normalized age (months)") ytitle("Pr(At notifying firm)") 	///
			  text(.35 -22   "{&beta}=`b'***" "(`se')"  ,place(right)  )
		graph export "$agegraphpath/RD/RD_`outcome'_m`t'.pdf", replace
	restore	
}
}
*
{ // Pr(other firm | m+6 , m+12)  
*===============================================================================
*NOTE: Think about how to give priority to other firms
forvalues t = 6(6)12 {
	preserve
		rename otherfirm2 otherfirm2_prio
		gen otherfirm2 = atfirm2==0 & otherfirm2_prio==1 
		local outcome "otherfirm2"

		keep  if eventtime_def==`t'
		*==========
		xtset   ym_def
		*==========

		eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
		local b  = round(_b[$instrument] ,.001)
		local se = round(_se[$instrument] ,.001)		
		
		gen freq=1
		
		*Residulize year*moth fixed effects
		qui areg `outcome' $covariets, absorb(ym_def)
		predict e, residuals
		sum `outcome' 
		replace `outcome' = e + r(mean)
	
		
		gen temp =round(runvar*12)
		egen 	bin = cut(temp), at(-36(2)0)
		egen 	bin2 = cut(temp), at(1(2)37)
		replace bin = bin2 if bin==.
		replace bin = bin + .5 
		bys bin: egen `outcome'_bin = mean(`outcome')
		qui tab runvar bin
		
		collapse (mean) `outcome' `outcome'_bin  (sum) freq, by(runvar bin treat)
		
		replace runvar = runvar*12
		
		*Linear Fit
		twoway 	(scatter `outcome'_bin  bin 		 	 , ms(o) mcolor(black))  		///	 	
			(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
			(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
			, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
			  xscale(r(-36 36)) xlabel(-36(12)36) 	name(t`t',replace)					///
			  xtitle("Normalized age (months)") ytitle("Pr(at new firm )") 		///
			  text(.47 22   "{&beta}=`b'" "(`se')"  ,place(right)  )
		graph export "$agegraphpath/RD/RD_`outcome'_m`t'.pdf", replace
	restore	
}
}
*
{ // Pr(Non-employment| m+6 , m+12)
*===============================================================================

forvalues t = 6(6)12 {
	preserve
		gen nonemp = working2==0
		local outcome "nonemp"
		*==========
		xtset   ym_def
		*==========

		eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
		local b  = round(_b[$instrument] ,.001)
		local se = round(_se[$instrument] ,.001)		

		keep  if eventtime_def==`t'
		gen freq=1
		
		*Residulize year*moth fixed effects
		qui areg `outcome' $covariets, absorb(ym_def)
		predict e, residuals
		sum `outcome' 
		replace `outcome' = e + r(mean)
		
		gen temp =round(runvar*12)
		egen 	bin = cut(temp), at(-36(2)0)
		egen 	bin2 = cut(temp), at(1(2)37)
		replace bin = bin2 if bin==.
		replace bin = bin + .5 
		bys bin: egen `outcome'_bin = mean(`outcome')
		qui tab runvar bin
		
		collapse (mean) `outcome' `outcome'_bin  (sum) freq, by(runvar bin treat)
		
		replace runvar = runvar*12
		
		*Linear Fit
		twoway 	(scatter `outcome'_bin  bin 		 	 , ms(o) mcolor(black))  		///	 	
			(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
			(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
			, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
			  xscale(r(-36 36)) xlabel(-36(12)36) 	name(t`t',replace)					///
			  xtitle("Normalized age (months)") ytitle("Pr(non-employment)") 	///
			  text(.37 22   "{&beta}=`b'***" "(`se')"  ,place(right)  )		
		graph export "$agegraphpath/RD/RD_`outcome'_m`t'.pdf", replace
	restore	
}
}
*
{ // Tenure at notification
*===============================================================================
preserve
	keep if eventtime_def==0
	local outcome "tenureatnot"
	*keep if year==2015

	*==========
	xtset   ym_def
	*==========
	eststo: xtreg 	`outcome'  	$instrument 	$linear  		, cluster(varselid) fe nonest
	local b  = round(_b[$instrument] ,.001)
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	
	
	gen temp =round(runvar*12)
	egen 	bin = cut(temp), at(-36(2)0)
	egen 	bin2 = cut(temp), at(1(2)37)
	replace bin = bin2 if bin==.
	replace bin = bin + .5 
	bys bin: egen `outcome'_bin = mean(`outcome')
	*tab runvar bin
	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	collapse (mean) `outcome' `outcome'_bin  (sum) freq, by(runvar bin treat)
	
	replace runvar = runvar*12
	
	*Linear Fit
	twoway 	(scatter `outcome'_bin  bin 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Tenure at notification (months)") ///
		   text(105 -24   "{&beta}=`b'**" "(`se')"  ,place(right)  ) name(A,replace)
	graph export "$agegraphpath/RD/RD_`outcome'.pdf", replace
restore	
	
}
*
******************************
*****	DONUT GRAPHS 	******
******************************
replace runvar = round(runvar*12)
drop if inrange(runvar,-3,3)

{ // Annual earnings (t+1) 			: Donut-RD (discrete bins) 
*===============================================================================
preserve
	
	keep if eventtime_def==12
	local outcome "annual_ear"
	replace `outcome' = `outcome'/1000
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = round(_b[$instrument] ,.001)
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	

	*tab runvar bin
	
	
	
	collapse (mean) `outcome'  (sum) freq, by(runvar treat)
	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	*Linear Fit
	twoway 	(scatter `outcome'   runvar		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Annual earnings (t+1) (1,000 SEK)") 	///
		  text(320 1   "{&beta}=`b'***" "(`se')"  ,place(right)  ) 
	graph export "$agegraphpath/RD/RD_`outcome'_t1_donutRD.pdf", replace
restore		
}
*
{ // Notification time 				: Donut-RD (discrete bins) 
*===============================================================================
preserve

	keep if eventtime_def==0
	local outcome "nottime_def"
	replace `outcome' = `outcome'
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = round(_b[$instrument] ,.001)
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	

	*tab runvar bin
	
	
	
	collapse (mean) `outcome'  (sum) freq, by(runvar treat)
	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	*Linear Fit
	twoway 	(scatter `outcome'   runvar		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Notification time (months)") 	///
		  text(8 -12   "{&beta}=`b'***" "(0`se')"  ,place(right)  ) 
	graph export "$agegraphpath/RD/RD_`outcome'_donutRD_month.pdf", replace
restore		
}
*
{ // Months in non-employment within 2 years 	: Donut-RD (discrete bins) 
*===============================================================================
preserve

	keep if eventtime_def==1
	local outcome "cumnonemployed"

	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = round(_b[$instrument] ,.001)
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	


	
	
	collapse (mean) `outcome'  (sum) freq, by(runvar treat)
	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	
	*Linear Fit
	twoway 	(scatter `outcome' runvar		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Months in non-employment" "(within 2 years of notification") ///
		   text(7.2 3   "{&beta}=`b'***" "(0`se')"  ,place(right)  )
	graph export "$agegraphpath/RD/RD_`outcome'_donutRD.pdf", replace
restore		
}
*
{ // Employment notifying firm within 2 years 	: Donut-RD (discrete bins) 
*===============================================================================
preserve
	keep if eventtime_def==1
	local outcome "cumatfirm2"
	
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = round(_b[$instrument] ,.001)
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	

	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	collapse (mean) `outcome'   (sum) freq, by(runvar treat)
	
	*Linear Fit
	twoway 	(scatter `outcome' runvar	 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Months worked at notifying firm" "(within 2 years of notification") ///
		   text(9 -14   "{&beta}=`b'***" "(0`se')"  ,place(right)  )
	graph export "$agegraphpath/RD/RD_`outcome'_donutRD.pdf", replace
restore	
	
}
*
{ // Employment other firm within 2 years 	: Donut-RD (discrete bins) 
*===============================================================================
preserve
	keep if eventtime_def==1
	local outcome "cumotherfirm2"
	
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = abs(round(_b[$instrument] ,.001))
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	

	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	collapse (mean) `outcome' (sum) freq, by(runvar treat)
	
	
	
	*Linear Fit
	twoway 	(scatter `outcome' runvar		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Months worked at new firm" "(within 2 years of notification") ///
		   text(7.5 -14   "{&beta}=-0`b'" "(0`se')"  ,place(right)  )
	graph export "$agegraphpath/RD/RD_`outcome'_donutRD.pdf", replace
restore	
	
}
*
{ // Months in non-employment within 2 years 	: Donut-RD (discrete bins) 
*===============================================================================
preserve

	keep if eventtime_def==1
	local outcome "cumnonemployed"

	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = round(_b[$instrument] ,.001)
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	

	
	collapse (mean) `outcome'  (sum) freq, by(runvar treat)
	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	*Linear Fit
	twoway 	(scatter `outcome'   runvar		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Months in non-employment" "(within 2 years of notification") ///
		   text(7.2 3   "{&beta}=`b'***" "(0`se')"  ,place(right)  )
	graph export "$agegraphpath/RD/RD_`outcome'_donutRD.pdf", replace
restore		
}
*	
{ // Months in unemployment within 2 years 	: Donut-RD (discrete bins) 
*===============================================================================
preserve
	keep if eventtime_def==1
	local outcome "cumunemployed"
	
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = abs(round(_b[$instrument] ,.001))
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	collapse (mean) `outcome'   (sum) freq, by(runvar treat)
	
	
	*Linear Fit
	twoway 	(scatter `outcome' runvar 		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Months in unemployemnt" "(within 2 years of notification") ///
		   text(5 12   "{&beta}=-0`b'*" "(0`se')"  ,place(right)  )
	graph export "$agegraphpath/RD/RD_`outcome'_donutRD.pdf", replace
restore	
	
}
*
{ // Months out of LF within 2 years 		: Donut-RD (discrete bins) 
*===============================================================================
preserve
	keep if eventtime_def==1
	local outcome "cumoutofLF"
	
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = abs(round(_b[$instrument] ,.001))
	local se = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)

	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	collapse (mean) `outcome'  (sum) freq, by(runvar treat)
	
	
	
	*Linear Fit
	twoway 	(scatter `outcome' runvar		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Months out of labor force" "(within 2 years of notification") ///
		   text(2.5 12   "{&beta}=-0`b'***" "(0`se')"  ,place(right)  )
	graph export "$agegraphpath/RD/RD_`outcome'_donutRD.pdf", replace
restore	
	
}
*

******************************************************
*****	DONUT GRAPHS INC. 2ND POLY ESTIMATE	******
******************************************************

{ // Annual earnings (t+1) Donut-RD (discrete bins) inc. 2nd order ploy
*===============================================================================
preserve
	
	keep if eventtime_def==12
	local outcome "annual_ear"
	replace `outcome' = `outcome'/1000
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = round(_b[$instrument] ,.001)
	local se = round(_se[$instrument] ,.001)
	
	eststo: xtreg 	`outcome'  	$instrument 	$quadratic  $covariets 		, cluster(varselid) fe nonest
	local b2  = round(_b[$instrument] ,.001)
	local se2 = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	

	*tab runvar bin
	
	
	
	collapse (mean) `outcome'  (sum) freq, by(runvar treat)
	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	*Linear Fit
	twoway 	(scatter `outcome'   runvar		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		(qfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(maroon) lpattern(dash)) 	 		///
		(qfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(maroon) lpattern(dash))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Annual earnings (t+1) (1,000 SEK)") 	///
		  text(320 1   "{&beta}=`b'***" "(`se')"  ,place(right)  ) ///
		  text(308 1   "{&beta}=`b2'*" "(`se2')"  ,place(right) color(maroon)  )
	graph export "$agegraphpath/RD/RD_`outcome'_t1_donutRD_incploy.pdf", replace
restore		
}
*
{ // Notification time (t+1) Donut-RD (discrete bins) inc. 2nd order ploy
*===============================================================================
preserve
	
	keep if eventtime_def==0
	local outcome "nottime_def"
	*replace `outcome' = `outcome'/30
	*==========
	xtset   ym_def
	*==========

	eststo: xtreg 	`outcome'  	$instrument 	$linear  $covariets 		, cluster(varselid) fe nonest
	local b  = round(_b[$instrument] ,.001)
	local se = round(_se[$instrument] ,.001)
	
	eststo: xtreg 	`outcome'  	$instrument 	$quadratic  $covariets 		, cluster(varselid) fe nonest
	local b2  = round(_b[$instrument] ,.004)
	local se2 = round(_se[$instrument] ,.001)
	
	
	gen freq=1
	
	*Residulize year*moth fixed effects
	qui areg `outcome' $covariets , absorb(ym_def)
	predict e, residuals
	sum `outcome' 
	replace `outcome' = e + r(mean)
	

	*tab runvar bin
	
	
	
	collapse (mean) `outcome'  (sum) freq, by(runvar treat)
	
	
	local lineopt 	"lpattern(dash) lcolor(black) lstyle(foreground)"
	*Linear Fit
	twoway 	(scatter `outcome'   runvar		 	 , ms(o) mcolor(black))  		///	 	
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(black)) 	 		///
		(lfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(black))	 		///
		(qfit 	 `outcome' runvar [aweight=freq] 	if treat==0, lcolor(maroon) lpattern(dash)) 	 		///
		(qfit 	 `outcome' runvar [aweight=freq] 	if treat==1, lcolor(maroon) lpattern(dash))	 		///
		, xline(0 , `lineopt')  legend(off) graphregion(color(white)) 					///
		  xscale(r(-36 36)) xlabel(-36(12)36) 					///
		  xtitle("Normalized age (months)") ytitle("Notification time (days)") 	///
		  text(200 1   "{&beta}=`b'***" "(`se')"  ,place(right)  ) ///
		  text(177 1   "{&beta}=`b2'***" "(`se2')"  ,place(right) color(maroon)  )
	graph export "$agegraphpath/RD/RD_`outcome'_t1_donutRD_incploy.pdf", replace
restore		
}
*



log close

